package org.me.makeSprite;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Panel extends SurfaceView implements SurfaceHolder.Callback {
	private TutorialThread _thread;
	private int x = 20;
	private int y = 20;
	
	public Panel(Context context) {
		super(context);
		getHolder().addCallback(this);
		_thread = new TutorialThread(getHolder(), this);
		setFocusable(true);
		// TODO Auto-generated constructor stub
	}
	
	 @Override
        public void onDraw(Canvas canvas) {
            Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
            canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(_scratch, x - (_scratch.getWidth()/2),y-(_scratch.getHeight()/2), null);
        }
	 
	 @Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		 x = (int)event.getX();
		 y= (int)event.getY();
		 return true;
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		_thread.SetRunning(true);
		_thread.start();
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
		boolean retry = true;
		_thread.SetRunning(false);
		while(retry)
		{
			try
			{
				_thread.join();
				retry = false;
			}
			catch (Exception e) {
				// TODO: handle exception
			}
		}
		
	}

}
